﻿using System.Windows.Forms;
using VideoClub.Business;
using System;
using VideoClub.Entities;

namespace VideoClub.UI.Actores
{
    public partial class FrmActores : Form
    {
        public FrmActores()
        {
            InitializeComponent();
        }

        
        public void Refrescar()
        {
            dgvActores.DataSource = ActorBO.ObtenerTodosLosActores();
        }

        private void BtnAgregarClick(object sender, System.EventArgs e)
        {
            var frm = new FrmEditarActor();
            frm.posicionarAlta();
            frm.FormClosed += new FormClosedEventHandler(frmEditarActor_FormClosed);
            frm.Show();
        }

        private void frmEditarActor_FormClosed(object sender, FormClosedEventArgs e)
        {
            Refrescar();
        }

        private void Eliminar_Click(object sender, EventArgs e)
        {
            
            int IdActor = Convert.ToInt32(dgvActores.CurrentRow.Cells[0].Value);
            
            if(IdActor != 0)
            {
                DialogResult d = MessageBox.Show("Esta seguro que desea eliminar el actor?", "Eliminar", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
                if (d == DialogResult.Yes)
                {
                    ActorBO.eliminar(IdActor);
                }
                Refrescar();
            }
            else
            {
                MessageBox.Show("Debe Seleccionar un actor para eliminar.","Eliminar",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
            
        }

        private void Refresh_Click(object sender, EventArgs e)
        {
            String filtro = txtFiltroApellido.Text;
            dgvActores.DataSource = ActorBO.ObtenerActorPorFiltro(filtro);
        }


        private void modificacion()
        {
            int IdActor = Convert.ToInt32(dgvActores.CurrentRow.Cells[0].Value);
            var actorAModificar = new Actor();

            actorAModificar = ActorBO.ObtenerActorPorId(IdActor);

            var frm = new FrmEditarActor();
            frm.posicionarModificacion(actorAModificar);
            frm.FormClosed += new FormClosedEventHandler(frmEditarActor_FormClosed);
            frm.Show();

        }


        private void Modificar_Click(object sender, EventArgs e)
        {
            modificacion();
        }

        private void dgvActores_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            modificacion();
        }


        private void dgvActores_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F5)
            {
                Refrescar();
            }
        }

        private void FrmActores_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F5)
            {
                Refrescar();
            }
        }



    }
}
